import { Injectable } from '@angular/core';
import { ActivatedRoute } from '@angular/router';

import { SelectComponent } from '../../shared/component/ng2-select/select';
import { SelectItem } from '../../shared/component/ng2-select/select-item';
@Injectable()
export class CommonService {
  constructor() { }
  public setSelect(value: string, select: SelectComponent, defaultValue: boolean): void {
    let active: any;
    if (value && value !== '') {
      select.itemObjects.forEach((i: SelectItem) => {
        if (value === i.id) {
          active = i;
        }
      });
    }
    if (!defaultValue && !active) {
      return;
    }
    if (active) {
      select.active = [active];
    } else {
      select.active = select.itemObjects.length > 0 ? [select.itemObjects[0]] : [];
    }
    if (select.selected && select.active.length > 0) {
      select.selected.emit(select.active[0]);
    }
  }
  public setSelectOrphan(value: string, select: SelectComponent): void {
    let active: any;
    if (value && value !== '') {
      select.itemObjects.forEach((i: SelectItem) => {
        if (value === i.id) {
          active = i;
        }
      });
    }
    if (active) {
      select.active = [active];
    } else {
      select.active = select.itemObjects.length === 1 ? [select.itemObjects[0]] : [];
    }
    if (select.selected && select.active.length > 0) {
      select.doEvent('selected', select.active[0]);
      select.selected.emit(select.active[0]);
    }
  }
  public copy(target, source) {
    if ($.type(source) === 'array') {
      for (let i = 0; i < source.length; i++) {
        if (!target[i]) {
          target[i] = {};
        }
        this.copy(target[i], source[i]);
      }
    } else if ($.type(source) === 'object') {
      for (const i in source) {
        if (i === 'valForm') {
          continue;
        }
        if ($.type(source[i]) === 'object') {
          if (!target[i]) {
            target[i] = {};
          }
          this.copy(target[i], source[i]);
        } else if ($.type(source[i]) === 'array') {
          if (!target[i]) {
            target[i] = [];
          }
          this.copy(target[i], source[i]);
        } else {
          target[i] = source[i];
        }
      }
    }
  }
  public copyBreak(target, source, breakItems: Array<any>) {
    if ($.type(source) === 'array') {
      for (let i = 0; i < source.length; i++) {
        if (!target[i]) {
          target[i] = {};
        }
        this.copyBreak(target[i], source[i], breakItems);
      }
    } else if ($.type(source) === 'object') {
      for (const i in source) {
        if (breakItems.indexOf(i) !== -1) {
          continue;
        }
        if ($.type(source[i]) === 'object') {
          if (!target[i]) {
            target[i] = {};
          }
          this.copyBreak(target[i], source[i], breakItems);
        } else if ($.type(source[i]) === 'array') {
          if (!target[i]) {
            target[i] = [];
          }
          this.copyBreak(target[i], source[i], breakItems);
        } else {
          target[i] = source[i];
        }
      }
    }
  }
  public clearFormGroup(o): any {
    for (const v in o) {
      if (o[v]) {
        if (v === 'valForm') {
          o[v] = null;
          delete o[v];
        }
        if (typeof o[v] === 'object') {
          this.clearFormGroup(o[v]);
        }
      }
    }
  }
  public getCurrentURL(route: ActivatedRoute): string {
    let back = '';
    route.pathFromRoot.forEach((a: ActivatedRoute) => {
      if (a.routeConfig) {
        back += '/' + a.routeConfig.path;
      }
    });
    return back;
  }
  public isEqualObject(obj1: any, obj2: any, breakItems: Array<any>) {
    if (!obj2) {
      if (obj1 === undefined || obj1 === '') {
        return true;
      } else {
        return false;
      }
    }
    if (obj1 && JSON.stringify(obj1) !== '{}' && obj1.length !== 0 && (JSON.stringify(obj2) === '{}' || obj2.length === 0)) {
      return false;
    }
    if (obj1 instanceof Array) {
      for (let i = 0; i < obj1.length; i++) {
        const tmp1 = obj1[i];
        const tmp2 = obj2[i];
        if (!this.isEqualObject(tmp1, tmp2, breakItems)) {
          return false;
        }
      }
      for (let i = 0; i < obj2.length; i++) {
        const tmp1 = obj1[i];
        const tmp2 = obj2[i];
        if (!this.isEqualObject(tmp1, tmp2, breakItems)) {
          return false;
        }
      }
      return true;
    } else if (obj1 instanceof Object) {
      for (const i in obj1) {
        if (breakItems.indexOf(i) > -1) {
          continue;
        }
        if (!this.isEqualObject(obj1[i], obj2[i], breakItems)) {
          return false;
        }
      }
      for (const i in obj2) {
        if (breakItems.indexOf(i) > -1) {
          continue;
        }
        if (!this.isEqualObject(obj1[i], obj2[i], breakItems)) {
          return false;
        }
      }
      return true;
    } else {
      return obj1 === obj2;
    }
  }
  public clearObject(o): void {
    for (const v in o) {
      if (o[v]) {
        o[v] = null;
        delete o[v];
      }
    }
  }
}
